//
//  DeviceSetPwdVC.m
//  xychain_DM_iOS
//
//  Created by Jpwang on 2018/5/30.
//  Copyright © 2018年 Jpwang. All rights reserved.
//

#import "DeviceSetPwdVC.h"

@interface DeviceSetPwdVC () {
    BOOL isPwdShow;
    BOOL isConfirmShow;
    DeviceItem *mDeviceItem;
}

@property(weak, nonatomic) IBOutlet UIButton *backBt;

@property(weak, nonatomic) IBOutlet UIView *pwdView;
@property(weak, nonatomic) IBOutlet UIView *confirmView;
@property(weak, nonatomic) IBOutlet UITextField *pwdTf;
@property(weak, nonatomic) IBOutlet UITextField *confirmTf;
@property(weak, nonatomic) IBOutlet UIButton *pwdBt;
@property(weak, nonatomic) IBOutlet UIButton *confirmBt;
@property(weak, nonatomic) IBOutlet UIButton *operateBt;

@end

@implementation DeviceSetPwdVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setData:(DeviceItem *) deviceItem {
    mDeviceItem = deviceItem;
}

- (void)initView {
    self.pwdView.layer.cornerRadius = globalRadius;
    self.confirmView.layer.cornerRadius = globalRadius;
    [[self.backBt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
    [[self.pwdBt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        self->isPwdShow = !self->isPwdShow;
        [self.pwdBt setImage:[UIImage imageNamed:self->isPwdShow?@"img_eye_open":@"img_eye_close"] forState:UIControlStateNormal];
        [self.pwdTf setSecureTextEntry:!self->isPwdShow];
    }];
    [[self.confirmBt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        self->isConfirmShow = !self->isConfirmShow;
        [self.confirmBt setImage:[UIImage imageNamed:self->isPwdShow?@"img_eye_open":@"img_eye_close"] forState:UIControlStateNormal];
        [self.confirmTf setSecureTextEntry:!self->isConfirmShow];
    }];
    [[self.operateBt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        NSString *pwd = self.pwdTf.text;
        NSString *confirm = self.confirmTf.text;
        if (pwd.length<=0) {
            [JPToast showText:@"请输入新密码"];
            return;
        }
        if (pwd.length<6 || pwd.length>18) {
            [JPToast showText:@"请输入6-18个字符的新密码"];
            return;
        }
        if (![pwd isEqualToString:confirm]) {
            [JPToast showText:@"两次输入的密码不一致"];
            return;
        }
        [self request:pwd];
    }];
}

- (void)request:(NSString *)pwd {
    [self showLoading:@"提交中"];
    NSString *url = [NSString stringWithFormat:@"%@%@/cmd", d_cmd, mDeviceItem.sn];
    NSDictionary *dic = @{@"cmd":@"update_password", @"content": pwd};
    [[[HttpRequest shareInstance] signPost:url map:[NSMutableDictionary dictionaryWithDictionary:dic]] subscribeNext:^(id x) {
        [self hideLoading];
        if ([x[@"code"] intValue] == 200) {
            [JPToast showText:@"修改密码成功"];
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [JPToast showText:x[@"message"]];
        }
    } error:^(NSError *error) {
        [self hideLoading];
        [JPToast showText:@"网络错误"];
    }];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

@end
